$().ready(function() {
    validateRule();
    layui.use(['form','laydate','layedit',], function() {
        var form = layui.form, layedit = layui.layedit,laydate = layui.laydate;
        var surveyInfoIndex = layedit.build('surveyInfo'); //建立编辑器

        laydate.render({
            elem: '#beginTime',
            type: 'date',
            format:'yyyy-MM-dd',
            isInitValue: false,
        });
        laydate.render({
            elem: '#endTime',
            type: 'date',
            format:'yyyy-MM-dd',
            isInitValue: false,
        });

        //提交时把值同步到文本域中
        form.verify({
            //content富文本域中的lay-verify值
            surveyInfo: function (value) {
                return layedit.sync(surveyInfoIndex);
            }
        });

        //监听提交
        form.on('submit(demo1)', function(data){
            console.log('data.field',data.field)
            data.field.surveyInfo = layedit.getContent(surveyInfoIndex);
            console.log('data.field  111',data.field)

            var url = ctx +"module/survey/" + ($('#signupForm').find("#id").val() == '' ? "save" : "update");
            $.ajax({
                cache : true,
                type : "POST",
                url : url,
                data : data.field,// 你的formid
                async : false,
                error : function(request) {
                    parent.layer.alert("Connection error");
                },
                success : function(data) {
                    if (data.code == 0) {
                        parent.layer.msg("操作成功");
                        parent.reLoad();
                        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                        parent.layer.close(index);

                    } else {
                        parent.layer.alert(data.msg)
                    }
                }
            });
            return false;
        });

    });
});

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            questionTitle : {
                required : true
            }
        },
        messages : {
            questionTitle : {
                required : icon + "请输入调查主题"
            }
        }
    })
}
